package com.mondo.financialwallet.presentation.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mondo.financialwallet.R;

import org.w3c.dom.Text;

/**
 * 温馨提示
 */
public class TipsDialog extends Dialog {

    public static TipsDialog failure(Context context) {
        Resources resources = context.getResources();
        return new TipsDialog(context, R.drawable.ic_failure,
                resources.getColor(R.color.colorFailure));
    }

    public static TipsDialog success(Context context) {
        Resources resources = context.getResources();
        return new TipsDialog(context, R.drawable.ic_success,
                resources.getColor(R.color.colorSuccess));
    }

    public static TipsDialog warning(Context context) {
        Resources resources = context.getResources();
        return new TipsDialog(context, R.drawable.ic_warning,
                resources.getColor(R.color.colorWarning));
    }


    private TextView mTitleTextView, mMessageTextView;
    private TextView mNegativeButton, mPositiveButton;

    private TipsDialog(Context context, @DrawableRes int drawableRes, @ColorInt int color) {
        super(context);

        ViewGroup root = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.dialog_tips_view, null);
        addContentView(root, new LinearLayout.LayoutParams(getDialogWidth(context),
                LinearLayout.LayoutParams.MATCH_PARENT));

        ((ImageView) root.findViewById(R.id.dialog_status_image_view))
                .setImageResource(drawableRes);

        // 标题
        mTitleTextView = root.findViewById(R.id.dialog_title_text);
        mTitleTextView.setTextColor(color);

        // 消息
        mMessageTextView = root.findViewById(R.id.dialog_message_text);

        mNegativeButton = root.findViewById(R.id.dialog_negative_button);
        mPositiveButton = root.findViewById(R.id.dialog_positive_button);
    }

    public TipsDialog build(@StringRes int title, @StringRes int message) {
        mTitleTextView.setText(title);
        mMessageTextView.setText(message);
        mNegativeButton.setText(R.string.dialog_all_right);
        mNegativeButton.setOnClickListener(v -> dismiss());
        return this;
    }

    public TipsDialog build(
            @StringRes int title, @StringRes int message, View.OnClickListener positiveListener) {
        this.build(title, message);
        mNegativeButton.setText(R.string.dialog_cancel);
        mPositiveButton.setText(R.string.dialog_next);
        mPositiveButton.setVisibility(View.VISIBLE);
        mPositiveButton.setOnClickListener(v->{
            positiveListener.onClick(v);
            dismiss();
        });

        return this;
    }

    private int getDialogWidth(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return (int) (dm.widthPixels * .85F);
    }
}
